﻿// Type: GalaSoft.MvvmLight.Command.RelayCommand
// Assembly: GalaSoft.MvvmLight.SL5, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c114982fcf1a3a2e
// Assembly location: D:\DLL\GalaSoft.MvvmLight.SL5.dll

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Input;

namespace GalaSoft.MvvmLight.Command
{
  public class RelayCommand : ICommand
  {
    private readonly Action _execute;
    private readonly Func<bool> _canExecute;
    private EventHandler CanExecuteChanged;

    public event EventHandler CanExecuteChanged
    {
      add
      {
        EventHandler eventHandler = this.CanExecuteChanged;
        EventHandler comparand;
        do
        {
          comparand = eventHandler;
          eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.CanExecuteChanged, comparand + value, comparand);
        }
        while (eventHandler != comparand);
      }
      remove
      {
        EventHandler eventHandler = this.CanExecuteChanged;
        EventHandler comparand;
        do
        {
          comparand = eventHandler;
          eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.CanExecuteChanged, comparand - value, comparand);
        }
        while (eventHandler != comparand);
      }
    }

    public RelayCommand(Action execute)
      : this(execute, (Func<bool>) null)
    {
    }

    public RelayCommand(Action execute, Func<bool> canExecute)
    {
      if (execute == null)
        throw new ArgumentNullException("execute");
      this._execute = execute;
      this._canExecute = canExecute;
    }

    public void RaiseCanExecuteChanged()
    {
      EventHandler eventHandler = this.CanExecuteChanged;
      if (eventHandler == null)
        return;
      eventHandler((object) this, EventArgs.Empty);
    }

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
      if (this._canExecute != null)
        return this._canExecute();
      else
        return true;
    }

    public void Execute(object parameter)
    {
      if (!this.CanExecute(parameter))
        return;
      this._execute();
    }
  }
}
